package com.kmzx.one.project.service.impl;

import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.ChannelActivity;
import com.kmzx.one.project.entity.ChannelBrowsing;
import com.kmzx.one.project.mapper.ExpandUserMapper;
import com.kmzx.one.project.service.ExpandUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.concurrent.TimeUnit;


@Service
public class ExpandUserServiceImpl implements ExpandUserService {


    @Autowired
    ExpandUserMapper expandUserMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @Async
    @Override
    public void checkExpandUser(String unionId, String prodId, String channelId) {
        insertExpandUserLog(channelId, null, prodId, unionId);
    }

    @Async
    @Override
    @Transactional
    public void insertExpandUserLog(String channelId, String userId, String prodId, String chUnionId) {


        String hasUnionId = stringRedisTemplate.opsForValue().get("><".concat(chUnionId));

        ChannelActivity channelActivity = expandUserMapper.selectChannelActivityById(channelId);
        Integer visitNumDay = channelActivity.getVisitNumDay();

        if (hasUnionId != null && !"".equals(hasUnionId)) {

        } else {

            LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
            LocalDateTime now = LocalDateTime.now();
            long between = Duration.between(now, today_end).get(ChronoUnit.SECONDS);
            stringRedisTemplate.opsForValue().setIfAbsent("><".concat(chUnionId), chUnionId, between, TimeUnit.SECONDS);
            channelActivity.setVisitNumDay(visitNumDay + 1);

            Month month = now.getMonth();
            int value = month.getValue();
            Date monthValue = expandUserMapper.checkUserBrowsingMonth(chUnionId);
            LocalDateTime monthValueTime = LocalDateTime.ofInstant(monthValue.toInstant(), ZoneId.systemDefault());
            int value1 = monthValueTime.getMonth().getValue();

            if (value > value1) {
                channelActivity.setVisitNumMonth(channelActivity.getVisitNumMonth() + 1);
            }
            ChannelBrowsing browsing = new ChannelBrowsing();
            browsing.setId(UUIDGenerator.generate())
                    .setUnionId(chUnionId)
                    .setBrowsingTime(new Date())
                    .setChannelId(channelId);
            if (userId != null) {
                browsing.setUserId(userId);
            } else {
                browsing.setUserId(null);
            }

            expandUserMapper.insertChannelBrowsing(browsing);
//

            expandUserMapper.updateChannelActivity2(channelActivity);
        }
        expandUserMapper.updateChannelActivity2(channelActivity);

    }


    @Async
    @Override
    @Transactional
    public void addExpandUserLog(String unionId, String chCode, String prodId, String userId) {


        int countNum = expandUserMapper.countUserBrowsing(unionId);


        ChannelActivity channelActivity = expandUserMapper.selectChannelActivityById(chCode);

        Integer visitNum = channelActivity.getVisitNum();
        Integer visitNumDay = channelActivity.getVisitNumDay();
        Integer visitNumMonth = channelActivity.getVisitNumMonth();
        LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        LocalDateTime now = LocalDateTime.now();
        long between = Duration.between(now, today_end).get(ChronoUnit.SECONDS);
        if (countNum == 0) {
            visitNum += 1;
            visitNumDay += 1;
            visitNumMonth += 1;
            channelActivity.setVisitNum(visitNum);
            channelActivity.setVisitNumDay(visitNumDay);
            channelActivity.setVisitNumMonth(visitNumMonth);
            expandUserMapper.updateChannelActivity2(channelActivity);

            ChannelBrowsing browsing = new ChannelBrowsing();
            browsing.setId(UUIDGenerator.generate())
                    .setUnionId(unionId)
                    .setBrowsingTime(new Date())
                    .setChannelId(chCode);
            expandUserMapper.insertChannelBrowsing(browsing);


            stringRedisTemplate.opsForValue().setIfAbsent("><".concat(unionId), unionId, between, TimeUnit.SECONDS);
        } else {
            String hasUnionId = stringRedisTemplate.opsForValue().get("><".concat(unionId));
            if (hasUnionId != null && !"".equals(hasUnionId)) {

            } else {
                stringRedisTemplate.opsForValue().setIfAbsent("><".concat(unionId), unionId, between, TimeUnit.SECONDS);
                channelActivity.setVisitNumDay(visitNumDay + 1);
                Date monthValue = expandUserMapper.checkUserBrowsingMonth(unionId);
                LocalDateTime monthValueTime = LocalDateTime.ofInstant(monthValue.toInstant(), ZoneId.systemDefault());
                int value1 = monthValueTime.getMonth().getValue();
                Month month = now.getMonth();
                int value = month.getValue();
                if (value > value1) {
                    channelActivity.setVisitNumMonth(channelActivity.getVisitNumMonth() + 1);
                }
                expandUserMapper.updateChannelActivity2(channelActivity);
            }
        }
    }


}
